package com.zr.service.dispatch.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zr.dao.dispatch.DispatchDelayRecordDao;
import com.zr.service.dispatch.DispatchDelayRecordService;
import com.zr.vo.dispatch.DispatchDelayRecord;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
@Service
public class DispatchDelayRecordImpl implements DispatchDelayRecordService {

    @Resource
    private DispatchDelayRecordDao dispatchDelayRecordDao;

    /**
     * 获得延误记录表
     * @author 杜雨萌
     * @param dispatchDelayRecord
     * @return
     */
    @Override
    public PageInfo<DispatchDelayRecord> list(DispatchDelayRecord dispatchDelayRecord) {
        PageHelper.startPage(dispatchDelayRecord.getPageNum(),dispatchDelayRecord.getPageSize());
        List<DispatchDelayRecord> list = dispatchDelayRecordDao.list(dispatchDelayRecord);
        for(DispatchDelayRecord d :list){
            if(d.getDispatchRoute() != null){
                d.setTransNode("中转:" + d.getDispatchTicket().getEndAreaName());
            }else{
                d.setTransNode("终点:" + d.getDispatchTicket().getEndAreaName());
            }
        }
        return new PageInfo<DispatchDelayRecord>(list);
    }
    /** 查询最近7天的延误数量
     * @return
     */
    @Override
    public Map getTicketDelayRecentWeek() {
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        //调整到7天以前
        calendar.add(Calendar.DAY_OF_MONTH,-6);
        Map<String,Object> map=new HashMap();
        List<String> dayList=new ArrayList<>();
        List<Integer> numList=new ArrayList<>();
        for (int i = 0; i < 7; i++) {
            Date date=calendar.getTime();
            Integer result=dispatchDelayRecordDao.getNumByDate(date);
            numList.add(result);
            //循环7次获取7天
            Integer month=calendar.get(Calendar.MONTH)+1;
            Integer day=calendar.get(Calendar.DAY_OF_MONTH);
            String str=month+"-"+day;
            dayList.add(str);
            //每次日期+1
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        map.put("sdata",numList);
        map.put("xdata",dayList);
        return map;
    }

    @Override
    public Map getTicketDelayRecentMonth() {
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        Date end=calendar.getTime();
        //调整到上个月
        calendar.add(Calendar.MONTH,-1);
        calendar.add(Calendar.DAY_OF_MONTH,1);
        Date begin=calendar.getTime();
        int j=(int) ((end.getTime() - begin.getTime()) / (1000*3600*24))+1;
        Map<String,Object> map=new HashMap();
        List<String> dayList=new ArrayList<>();
        List<Integer> numList=new ArrayList<>();
        for (int i = 0; i < j; i++) {
            Date date=calendar.getTime();
            Integer result=dispatchDelayRecordDao.getNumByDate(date);
            numList.add(result);
            //循环j次获取j天
            Integer month=calendar.get(Calendar.MONTH)+1;
            Integer day=calendar.get(Calendar.DAY_OF_MONTH);
            String str=month+"-"+day;
            dayList.add(str);
            //每次日期+1
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        map.put("sdata",numList);
        map.put("xdata",dayList);
        return map;
    }

    @Override
    public Map getTicketDelayThisYear() {
        //获取日历对象
        Calendar calendar = Calendar.getInstance();
        int j=calendar.get(Calendar.MONTH)+1;
        //调整到1月
        calendar.set(Calendar.MONTH,0);
        Map<String,Object> map=new HashMap();
        List<String> monthList=new ArrayList<>();
        List<Integer> numList=new ArrayList<>();
        for (int i = 0; i < j; i++) {
            Date date=calendar.getTime();
            Integer result=dispatchDelayRecordDao.getNumByMonth(date);
            numList.add(result);
            //循环j次获取j个月
            Integer year=calendar.get(Calendar.YEAR);
            Integer month=calendar.get(Calendar.MONTH)+1;
            String str=year+"-"+month;
            monthList.add(str);
            //每次月份+1
            calendar.add(Calendar.MONTH,1);
        }
        map.put("sdata",numList);
        map.put("xdata",monthList);
        return map;
    }
}
